# Copyright (C) 2018 Fondazione Istituto Italiano di Tecnologia
#
# Licensed under either the GNU Lesser General Public License v3.0 :
# https://www.gnu.org/licenses/lgpl-3.0.html
# or the GNU Lesser General Public License v2.1 :
# https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
# at your option.

cmake_minimum_required(VERSION 3.5)

# This sets the PROJECT_NAME, PROJECT_VERSION as well as other variable
# listed here: https://cmake.org/cmake/help/latest/command/project.html.
project(gazebo-fmi
        LANGUAGES C CXX
        VERSION 0.1.1)

# Defines the CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR and many other useful macros.
# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
include(GNUInstallDirs)

# Control where libraries and executables are placed during the build.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
if (CMAKE_VERSION VERSION_LESS 3.11)
  list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake-3.11)
endif() 

option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)

# Build test related commands?
option(BUILD_TESTING "Create tests using CMake" OFF)
if(BUILD_TESTING)
    enable_testing()
endif()

# Enable RPATH support for installed binaries and libraries
include(AddInstallRPATHSupport)
add_install_rpath_support(BIN_DIRS "${CMAKE_INSTALL_FULL_BINDIR}"
                          LIB_DIRS "${CMAKE_INSTALL_FULL_LIBDIR}"
                          INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}"
                          USE_LINK_PATH)

# Encourage user to specify a build type (e.g. Release, Debug, etc.), otherwise set it to Release.
if(NOT CMAKE_CONFIGURATION_TYPES)
    if(NOT CMAKE_BUILD_TYPE)
        message(STATUS "Setting build type to 'Release' as none was specified.")
        set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE "Release")
    endif()
endif()

# Find dependencies
find_package(gazebo REQUIRED)
if (gazebo_VERSION_MAJOR LESS 7.0)
    message(status "Gazebo version : " ${gazebo_VERSION_MAJOR}.${gazebo_VERSION_MINOR}.${gazebo_VERSION_PATCH})
    message(FATAL_ERROR "Your Gazebo version is older than Gazebo 7.0. Gazebo Yarp plugins are supported with gazebo versions >= 7.0. Please update to a newer version")
endif()

# Gazebo 7 does not export the C++11 requirement in its targets
if (gazebo_VERSION_MAJOR LESS 8.0)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GAZEBO_CXX_FLAGS}")
endif()

find_package(FMILibrary QUIET)
option(USE_SYSTEM_FMILIBRARY "If TRUE/ON use system FMILibrary, otherwise download and compile using FetchContent" ${FMILibrary_FOUND})
if (USE_SYSTEM_FMILIBRARY)
    find_package(FMILibrary REQUIRED)
else()
    include(FetchFMILibrary)
endif()

# Add extern libraries vendored (only used for the tests)
if(BUILD_TESTING)
  find_package(MATIO REQUIRED)
  add_subdirectory(extern)
endif()

# Add setup-examples.sh to easily test exampes
set(GAZEBO_FMI_EXAMPLES_INSTALL_DIR ${CMAKE_INSTALL_DATAROOTDIR}/gazebo-fmi/examples)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/setup-examples.sh.in ${PROJECT_BINARY_DIR}/setup-examples.sh)
install(FILES ${PROJECT_BINARY_DIR}/setup-examples.sh DESTINATION ${GAZEBO_FMI_EXAMPLES_INSTALL_DIR})

# Add support libraries
add_subdirectory(libraries)

# Add plugins
add_subdirectory(plugins)

include(InstallBasicPackageFiles)
install_basic_package_files(${PROJECT_NAME}
                            VERSION ${${PROJECT_NAME}_VERSION}
                            COMPATIBILITY AnyNewerVersion
                            TARGETS_PROPERTY ${PROJECT_NAME}_TARGETS
                            VARS_PREFIX ${PROJECT_NAME}
                            NO_CHECK_REQUIRED_COMPONENTS_MACRO
                            DEPENDENCIES "gazebo")



# Add the uninstall target
include(AddUninstallTarget)

